<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\controller\ApiBase;
class Login extends AdminBase
{
    public function login()
    {
        return $this->view->fetch('Login/login');
//        return $this->autoChacheView('Share/floder');
//        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function loginApi(){
//        $apiObj = new ApiBase();
        $email = $this->request->param('email');
        $password = $this->request->param('pwd');
        $data = [
            'email' => $email,
            'password'=>$password,
        ];
        $validate = new \app\common\validate\User;
        if (!$validate->scene('mailLogin')->check($data)) {
            ApiBase::error_static($validate->getError());
        }
        if ($email != '272604545@qq.com'){
            ApiBase::error_static('账户或密码错误！');
        }
        $user = \app\common\model\User::getByEmail($email);
        if (!$user){
            ApiBase::error_static('用户信息有误！');
        }
        if ($user -> password != md5($password)){
            ApiBase::error_static('用户信息有误！');
        }
        $uid = $user->id;
        $token = \yu\Random::uuid_yu();
//        ApiBase::success_static('登录成功！',$token);
        //token设置有效期为30天
        $t = \cache('admin-login-token',$token,3600);
        $u = \cache('admin-login-uid',$uid,3600);
//        \cache($token,$uid,60*60*24*30,'admin-login-token');
        $result['token'] = $token;
        $result['user']=$user;
        $result['t']=$t;
        $result['u']=$u;
        ApiBase::success_static('登录成功！',$result,200);

    	// $this->show('sdfds');
//    	$this->debugBegain();
//    	$this->showEnv();
//    	$this->debugEnd();
//    	$this->debugTimeShow();
    	// var_dump($this->request);
//    	return '';
    }
}
